#include "serverwidget.h"
#include "ui_serverwidget.h"
#include "widget.h"
#include "ui_widget.h"
#include <QTcpServer>
#include <QMessageBox>
#include <QHostAddress>
#include <QPushButton>
#include <QTextCodec>
#include <QString>
#include <QDebug>
#include <QTextEdit>
#include <QDialog>
#include <QMovie>
#include <QStringList>
#include <QCompleter>
#include <QHBoxLayout>
#include <QIcon>
#include <QMouseEvent>
#include <QPoint>
#include <QPixmap>
#include <QGraphicsDropShadowEffect>
#include <QFrame>
#include <QColor>
#include <QTimer>
#include <QString>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QPushButton>
#include<QTime>
#include <QThread>
#include <QStringList>

int question_id ;
//QString right_text = "Right+1" ;
int people_que_id ;
QString sendt ;

ServerWidget::ServerWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ServerWidget)
{
    ui->setupUi(this);
    setWindowTitle("时态抢答交互系统");//设置窗口标题
    setWindowIcon(QIcon("C:\\Users\\22509\\Pictures\\logo.jpg"));

    //label显示动态图片
    QMovie *movie=new QMovie();
    movie->setFileName("C:\\Users\\22509\\Pictures\\bg7.gif");
    ui->bg->setMovie(movie);
    movie->start();
    ui->bg->setScaledContents(true);//自适应


    QIcon icon;
    icon.addFile(tr("C:\\Users\\22509\\Pictures\\return.png"));
    ui->ReturnBtn->setIcon(icon);


    QStringList strList;
    strList<<"全部"<<"数电"<<"模电"<<"信号与系统"<<"自动控制原理"<<"EDA"<<"数据结构"<<"嵌入式"<<"DSP";
    ui->comboBox->addItems(strList);


    ui->StartBtn_2->setEnabled(false);
    ui->EndBtn_3->setEnabled(false);
    ui->SendBtn->setEnabled(false);
    ui->NextBtn->setEnabled(false);

    connect(ui->StartBtn,&QPushButton::clicked,this,[=](){
        ui->StartBtn_2->setEnabled(true);
        ui->EndBtn_3->setEnabled(true);
        ui->SendBtn->setEnabled(true);
        ui->NextBtn->setEnabled(true);
    });

    connect(ui->EndBtn_3,&QPushButton::clicked,this,[=](){
        ui->StartBtn_2->setEnabled(false);
        ui->EndBtn_3->setEnabled(false);
        ui->SendBtn->setEnabled(false);
        ui->NextBtn->setEnabled(false);
    });




    //创建通信套接字
    server=new QTcpServer(this);
}

ServerWidget::~ServerWidget()
{
    delete ui;
}



void ServerWidget::on_ListenBtn_clicked()
{
    /*ubwidget::final_score = 1 ;*/
    //获取当前服务端ip以及port
//    qa  = new QAWidget() ;
//    static  int time_1 = 3 ;   //修改时间
//    time_1 = 3 ;
    QTimer *timer1 = new QTimer(this) ;
          // timer1->start(2000) ;
           timer1->stop() ;

           QTimer *timer2 = new QTimer(this) ;
                  timer2->start(2000) ;
                  timer2->stop() ;

    QString ip=ui->IpEdit->text();
    quint16 port=ui->PortEdit->text().toUShort();//转换为无符号短整型
    //监听 listen
    //当ip地址不为空 port不为0时监听
    if(ip!=""&&port!=0)
    {
        server->listen(QHostAddress(ip),port);
        //将侦听按钮设置为不能点击
        ui->ListenBtn->setEnabled(false);
        //通过消息对话框 给一个提示 监听成功
        QMessageBox::information(this,"监听成功","监听成功");

        //有几个人连接成功
        //显示连接成功的编号
        //***
        connect(server,&QTcpServer::newConnection,[=](){
            socket = server->nextPendingConnection() ;
            save_socketlist.append(socket) ;
            QString client_ip = socket->peerAddress().toString();
            quint16 client_port = socket->peerPort();

            // ip 以及端口号 组包
            QString str = QString("[%1,%2]连接成功").arg(client_ip).arg(client_port) ;
//             qDebug() << save_socketlist.size() ;
            ui->textEdit->append(str) ;
            participants ++ ;

            QString str11 = QString("%1人").arg(participants) ;
          //  ui->textEdit->append(str1) ;
            ui->label_18->setText(str11) ;
//             ui->label_4->setText(str1) ;
            //ui->recetext

           //接收客户端数据
            connect(socket,&QTcpSocket::readyRead,[=](){



                     //读取数据
                    QByteArray arr = socket->readAll() ;

                    //转码
                    QString str_arr = QString(arr) ;


                     if(str_arr != "")
                     {


                     QTextCodec* codec = QTextCodec::codecForName("gbk") ;
                     QString arr_str = codec->toUnicode(arr) ;
                     receive_text = arr ;
                     ui->textEdit_3->clear() ;
                     ui->textEdit_3->append(arr_str) ;
                      QString single_text = ui->textEdit_3->toPlainText() ;
                        // 侦听数据判断

                       for(int i = 1 ; i < 100000000 ; i++) ;
                    if(single_text.contains("pree", Qt::CaseInsensitive))// 字符串包含
                     {
                        //pree+3
                        ui->label_17->setText("有") ;

                        QString pree_id = single_text.mid(5,1) ;
                        qDebug() << "preeid :" ;

//                        QString str2 = single_text.mid(7,1) ;
                         people_que_id = pree_id.toInt() ;
                                                 qDebug() << people_que_id  ;


                         QString peo_str = QString("%1").arg(people_que_id) ;
                          ui->label_15->setText(peo_str) ;
                         single_text.clear() ;
//                         timer1->start(1000) ;
//                         connect(timer1,&QTimer::timeout,[=](){


//                                  for(i)

//                                  QString str1 = QString("Yes+%1").arg() ;  // Yes+id

//                                    for(int j = 0 ; j < 50 ; j++) ;

                                    QThread::msleep(500) ;
                                  QString str2 = QString("Yes+%1").arg(people_que_id) ;
                                  for(int i = 0 ; i < save_socketlist.size() ; i++)
                                  {

                                       save_socketlist[i]->write(str2.toLocal8Bit()) ;

                                  }



//                             timer1->stop() ;
//                         }) ;





                     }


                     QThread::msleep(500) ;
                     if(single_text.contains("ansd", Qt::CaseInsensitive))
                     {

//                         single_text.clear() ;
                         str_arr.clear() ;

                           //ansd+1:A
                         QString str1 = single_text.mid(5,1) ;
                         QString str2 = single_text.mid(7,1) ;
                        qDebug() << "id" ;
                         qDebug() << str1 ;
                         qDebug() << "answer" ;
                            qDebug() << str2 ;

                            int this_part = str1.toInt() ;
                            if(( str2 == ui->answer_lb->text() ) && (str1.toInt()) == people_que_id)
                            {
                                score[this_part] ++ ;

                                QString q1 = QString("本题有%1号同学答对").arg(this_part) ;


                                sendt = QString("Right+%1").arg(question_id) ;
                                ui->label_13->setText("是") ;
                            }
                            else {
                                sendt  = QString("Wrong") ;
                                 ui->label_13->setText("否") ;

                            }


//                            timer2->start() ;
//                            connect(timer2,&QTimer::timeout,[=](){




                                QThread::msleep(500) ;
                                for(int i = 0 ; i < save_socketlist.size() ; i++)
                                {

                                     save_socketlist[i]->write(sendt.toLocal8Bit()) ;

                                }

//                                timer2->stop() ;
//                            }) ;

                     }




//                      if()
                     //


                     }



            });
        }) ;
        //***
        //显示有几个人连接成功
    }
}

void ServerWidget::on_NextBtn_clicked()
{
    //    qDebug()<
        ui->textEdit_2->clear();
        question_id ++ ;
        QSqlQuery query;
        QString str_que = QString("select * from question where id = %1").arg(question_id) ;
        query.exec(str_que );

        window2->ui->label_13->clear();
        window2->ui->label_14->clear();
        window2->ui->label_15->clear();
        window2->ui->label_17->clear();

        if(ui->comboBox->currentText()=="全部")
        {
            while(query.next())//一行一行去遍历数据
            {
                //打印数据
        //        int id = query.value(0).toInt();
                QString subject = query.value(1).toString();
                ui->subject_lb->setText(subject) ;
                QString content = query.value(2).toString();
                QString content2 = QString("第%1题").arg(question_id) ;
                ui->textEdit_2->append(content2);
                ui->textEdit_2->append(content);
                QString choice = query.value(3).toString();
                choice.replace(" A","A");
                choice.replace("  ","\n");
                ui->textEdit_2->append(choice) ;
                QString answer = query.value(4).toString();
                ui->answer_lb->setText(answer) ;
        //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

            }
        }

        if(ui->comboBox->currentText()=="数电")
        {
            while(query.next())//一行一行去遍历数据
            {

                 //打印数据
            //     int id = query.value(0).toInt();
                 QString subject = query.value(1).toString();

                 if(question_id>=1&&question_id<=5)
                 {
                     ui->subject_lb->setText(subject) ;
                    QString content = query.value(2).toString();
                    QString content2 = QString("第%1题").arg(question_id) ;
                    ui->textEdit_2->append(content2);
                    ui->textEdit_2->append(content);
                    QString choice = query.value(3).toString();
                    choice.replace(" A","A");
                    choice.replace("  ","\n");
                    ui->textEdit_2->append(choice) ;
                    QString answer = query.value(4).toString();
                    ui->answer_lb->setText(answer) ;
            //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

                }
                 else {
                     question_id=0;
                 }

            }
        }



        if(ui->comboBox->currentText()=="模电")
        {
            while(query.next())//一行一行去遍历数据
            {

                 //打印数据
            //     int id = query.value(0).toInt();
                 QString subject = query.value(1).toString();

                 if(question_id>5&&question_id<=10)
                 {
                     ui->subject_lb->setText(subject) ;
                    QString content = query.value(2).toString();
                    QString content2 = QString("第%1题").arg(question_id-5) ;
                    ui->textEdit_2->append(content2);
                    ui->textEdit_2->append(content);
                    QString choice = query.value(3).toString();
                    choice.replace(" A","A");
                    choice.replace("  ","\n");
                    ui->textEdit_2->append(choice) ;
                    QString answer = query.value(4).toString();
                    ui->answer_lb->setText(answer) ;
            //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

                }
                 else {
                     question_id=5;
                 }

            }
        }


        if(ui->comboBox->currentText()=="信号与系统")
        {
            while(query.next())//一行一行去遍历数据
            {

                 //打印数据
            //     int id = query.value(0).toInt();
                 QString subject = query.value(1).toString();

                 if(question_id>10&&question_id<=15)
                 {
                    ui->subject_lb->setText(subject) ;
                    QString content = query.value(2).toString();
                    QString content2 = QString("第%1题").arg(question_id-10) ;
                    ui->textEdit_2->append(content2);
                    ui->textEdit_2->append(content);
                    QString choice = query.value(3).toString();
                    choice.replace(" A","A");
                    choice.replace("  ","\n");
                    ui->textEdit_2->append(choice) ;
                    QString answer = query.value(4).toString();
                    ui->answer_lb->setText(answer) ;
            //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

                }
                 else {
                     question_id=10;
                 }

            }
        }



        if(ui->comboBox->currentText()=="自动控制原理")
        {
            while(query.next())//一行一行去遍历数据
            {

                 //打印数据
            //     int id = query.value(0).toInt();
                 QString subject = query.value(1).toString();

                 if(question_id>15&&question_id<=20)
                 {
                    ui->subject_lb->setText(subject) ;
                    QString content = query.value(2).toString();
                   QString content2 = QString("第%1题").arg(question_id-15) ;
                    ui->textEdit_2->append(content2);
                    ui->textEdit_2->append(content);
                    QString choice = query.value(3).toString();
                    choice.replace(" A","A");
                    choice.replace("  ","\n");
                    ui->textEdit_2->append(choice) ;
                    QString answer = query.value(4).toString();
                    ui->answer_lb->setText(answer) ;
            //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

                }
                 else {
                     question_id=15;
                 }

            }
        }



        if(ui->comboBox->currentText()=="EDA")
        {
            while(query.next())//一行一行去遍历数据
            {

                 //打印数据
            //     int id = query.value(0).toInt();
                 QString subject = query.value(1).toString();

                 if(question_id>20&&question_id<=25)
                 {
                    ui->subject_lb->setText(subject) ;
                    QString content = query.value(2).toString();
                   QString content2 = QString("第%1题").arg(question_id-20) ;
                    ui->textEdit_2->append(content2);
                    ui->textEdit_2->append(content);
                    QString choice = query.value(3).toString();
                    choice.replace(" A","A");
                    choice.replace("  ","\n");
                    ui->textEdit_2->append(choice) ;
                    QString answer = query.value(4).toString();
                    ui->answer_lb->setText(answer) ;
            //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

                }
                 else {
                     question_id=20;
                 }

            }
        }


        if(ui->comboBox->currentText()=="数据结构")
        {
            while(query.next())//一行一行去遍历数据
            {

                 //打印数据
            //     int id = query.value(0).toInt();
                 QString subject = query.value(1).toString();

                 if(question_id>25&&question_id<=30)
                 {
                    ui->subject_lb->setText(subject) ;
                    QString content = query.value(2).toString();
                   QString content2 = QString("第%1题").arg(question_id-25) ;
                    ui->textEdit_2->append(content2);
                    ui->textEdit_2->append(content);
                    QString choice = query.value(3).toString();
                    choice.replace(" A","A");
                    choice.replace("  ","\n");
                    ui->textEdit_2->append(choice) ;
                    QString answer = query.value(4).toString();
                    ui->answer_lb->setText(answer) ;
            //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

                }
                 else {
                     question_id=25;
                 }

            }
        }




        if(ui->comboBox->currentText()=="嵌入式")
        {
            while(query.next())//一行一行去遍历数据
            {

                 //打印数据
            //     int id = query.value(0).toInt();
                 QString subject = query.value(1).toString();

                 if(question_id>30&&question_id<=35)
                 {
                    ui->subject_lb->setText(subject) ;
                    QString content = query.value(2).toString();
                   QString content2 = QString("第%1题").arg(question_id-30) ;
                    ui->textEdit_2->append(content2);
                    ui->textEdit_2->append(content);
                    QString choice = query.value(3).toString();
                    choice.replace(" A","A");
                    choice.replace("  ","\n");
                    ui->textEdit_2->append(choice) ;
                    QString answer = query.value(4).toString();
                    ui->answer_lb->setText(answer) ;
            //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

                }
                 else {
                     question_id=30;
                 }

            }
        }



        if(ui->comboBox->currentText()=="DSP"
                "")
        {
            while(query.next())//一行一行去遍历数据
            {

                 //打印数据
            //     int id = query.value(0).toInt();
                 QString subject = query.value(1).toString();

                 if(question_id>35&&question_id<=40)
                 {
                    ui->subject_lb->setText(subject) ;
                    QString content = query.value(2).toString();
                   QString content2 = QString("第%1题").arg(question_id-35) ;
                    ui->textEdit_2->append(content2);
                    ui->textEdit_2->append(content);
                    QString choice = query.value(3).toString();
                    choice.replace(" A","A");
                    choice.replace("  ","\n");
                    ui->textEdit_2->append(choice) ;
                    QString answer = query.value(4).toString();
                    ui->answer_lb->setText(answer) ;
            //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

                }
                 else {
                     question_id=35;
                 }

            }
        }



            ui->textEdit_3->clear() ;

             for(int i = 1 ; i <= participants ; i++ )
            {
                QString    str = QString("%1号选手的得分为: %2").arg(i).arg(score[i]);
                ui->textEdit_3->append(str) ;

            }
}


//断开连接
void ServerWidget::on_pushbutton_clicked()
{
    ui->ListenBtn->setEnabled(true);
    if(socket == nullptr)
           return ;


       socket->disconnectFromHost() ;


       socket->close() ;  // 关闭套接字,并清空

       socket = nullptr ;

       //  清空
   //    ui->ListenBtn->setEnabled(true) ;
       participants = 0 ;

       for(int i = 0 ; i <= participants ; i++)
       {
           score[i] = 0 ;
       }
       final_winner = 0 ;

       ui->textEdit->clear() ;

      QString str1 = QString("%1人").arg(participants) ;

        ui->label_18->setText(str1) ;

               //       ui->label_4->setText(str1) ;
}
//发送题目
void ServerWidget::on_StartBtn_clicked()
{

    ui->textEdit_2->clear();
    QSqlDatabase db =QSqlDatabase::addDatabase("QSQLITE");
    //数据库名称
    db.setDatabaseName("question.db");

    bool ok = db.open();

        if (ok){
            qDebug() << "database connect is ok";
        } else {
            qDebug() << "database connect is fail";
        }



    QSqlQuery query;
    question_id = 1 ;
    QString str_que = QString("select * from question where id = %1").arg(question_id) ;
    query.exec(str_que );

    qDebug()<<ui->comboBox->currentText();
    if(ui->comboBox->currentText()=="全部")
    {
        while(query.next())//一行一行去遍历数据
        {
            //打印数据
    //        int id = query.value(0).toInt();
            QString subject = query.value(1).toString();
            ui->subject_lb->setText(subject) ;
            QString content = query.value(2).toString();
            QString content2 = QString("第%1题").arg(question_id) ;
            ui->textEdit_2->append(content2);
            ui->textEdit_2->append(content);
            QString choice = query.value(3).toString();
            choice.replace(" A","A");
            choice.replace("  ","\n");
            ui->textEdit_2->append(choice) ;
            QString answer = query.value(4).toString();
            ui->answer_lb->setText(answer) ;
    //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

        }
    }

    if(ui->comboBox->currentText()=="数电")
    {
        while(query.next())//一行一行去遍历数据
        {

             //打印数据
        //     int id = query.value(0).toInt();
             QString subject = query.value(1).toString();

             if(subject=="数电")
             {
                 ui->subject_lb->setText(subject) ;
                QString content = query.value(2).toString();
               QString content2 = QString("第%1题").arg(question_id) ;
                ui->textEdit_2->append(content2);
                ui->textEdit_2->append(content);
                QString choice = query.value(3).toString();
                choice.replace(" A","A");
                choice.replace("  ","\n");
                ui->textEdit_2->append(choice) ;
                QString answer = query.value(4).toString();
                ui->answer_lb->setText(answer) ;
        //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

            }

        }
    }



    if(ui->comboBox->currentText()=="模电")
    {
        question_id = 5 ;
        while(query.next())//一行一行去遍历数据
        {

             //打印数据
        //     int id = query.value(0).toInt();
             QString subject = query.value(1).toString();

             if(subject=="模电")
             {
                 ui->subject_lb->setText(subject) ;
                QString content = query.value(2).toString();
                QString content2 = QString("第%1题").arg(question_id-5) ;
                ui->textEdit_2->append(content2);
                ui->textEdit_2->append(content);
                QString choice = query.value(3).toString();
                choice.replace(" A","A");
                choice.replace("  ","\n");
                ui->textEdit_2->append(choice) ;
                QString answer = query.value(4).toString();
                ui->answer_lb->setText(answer) ;
        //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

            }

        }
    }


    if(ui->comboBox->currentText()=="信号与系统")
    {
        question_id = 10 ;
        while(query.next())//一行一行去遍历数据
        {


             //打印数据
        //     int id = query.value(0).toInt();
             QString subject = query.value(1).toString();

             if(subject=="信号与系统")
             {
                ui->subject_lb->setText(subject) ;
                QString content = query.value(2).toString();
                QString content2 = QString("第%1题").arg(question_id-10) ;
                ui->textEdit_2->append(content2);
                ui->textEdit_2->append(content);
                QString choice = query.value(3).toString();
                choice.replace(" A","A");
                choice.replace("  ","\n");
                ui->textEdit_2->append(choice) ;
                QString answer = query.value(4).toString();
                ui->answer_lb->setText(answer) ;
        //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

            }

        }
    }



    if(ui->comboBox->currentText()=="自动控制原理")
    {
        question_id = 15 ;
        while(query.next())//一行一行去遍历数据
        {


             //打印数据
        //     int id = query.value(0).toInt();
             QString subject = query.value(1).toString();

             if(subject=="自动控制原理")
             {
                ui->subject_lb->setText(subject) ;
                QString content = query.value(2).toString();
               QString content2 = QString("第%1题").arg(question_id-15) ;
                ui->textEdit_2->append(content2);
                ui->textEdit_2->append(content);
                QString choice = query.value(3).toString();
                choice.replace(" A","A");
                choice.replace("  ","\n");
                ui->textEdit_2->append(choice) ;
                QString answer = query.value(4).toString();
                ui->answer_lb->setText(answer) ;
        //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

            }

        }
    }



    if(ui->comboBox->currentText()=="EDA")
    {
        question_id = 20 ;
        while(query.next())//一行一行去遍历数据
        {


             //打印数据
        //     int id = query.value(0).toInt();
             QString subject = query.value(1).toString();

             if(subject=="EDA")
             {
                ui->subject_lb->setText(subject) ;
                QString content = query.value(2).toString();
               QString content2 = QString("第%1题").arg(question_id-20) ;
               ui->textEdit_2->append(content2);
                ui->textEdit_2->append(content);
                QString choice = query.value(3).toString();
                choice.replace(" A","A");
                choice.replace("  ","\n");
                ui->textEdit_2->append(choice) ;
                QString answer = query.value(4).toString();
                ui->answer_lb->setText(answer) ;
        //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

            }

        }
    }


    if(ui->comboBox->currentText()=="数据结构")
    {
       question_id = 25 ;
        while(query.next())//一行一行去遍历数据
        {


             //打印数据
        //     int id = query.value(0).toInt();
             QString subject = query.value(1).toString();

             if(subject=="数据结构")
             {
                ui->subject_lb->setText(subject) ;
                QString content = query.value(2).toString();
               QString content2 = QString("第%1题").arg(question_id-25) ;
                ui->textEdit_2->append(content2);
                ui->textEdit_2->append(content);
                QString choice = query.value(3).toString();
                choice.replace(" A","A");
                choice.replace("  ","\n");
                ui->textEdit_2->append(choice) ;
                QString answer = query.value(4).toString();
                ui->answer_lb->setText(answer) ;
        //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

            }

        }
    }




    if(ui->comboBox->currentText()=="嵌入式")
    {
       question_id = 30 ;
        while(query.next())//一行一行去遍历数据
        {


             //打印数据
        //     int id = query.value(0).toInt();
             QString subject = query.value(1).toString();

             if(subject=="嵌入式")
             {
                ui->subject_lb->setText(subject) ;
                QString content = query.value(2).toString();
               QString content2 = QString("第%1题").arg(question_id-30) ;
                ui->textEdit_2->append(content2);
                ui->textEdit_2->append(content);
                QString choice = query.value(3).toString();
                choice.replace(" A","A");
                choice.replace("  ","\n");
                ui->textEdit_2->append(choice) ;
                QString answer = query.value(4).toString();
                ui->answer_lb->setText(answer) ;
        //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

            }

        }
    }



    if(ui->comboBox->currentText()=="DSP"
            "")
    {
        question_id = 35 ;
        while(query.next())//一行一行去遍历数据
        {


             //打印数据
        //     int id = query.value(0).toInt();
             QString subject = query.value(1).toString();

             if(subject=="DSP")
             {
                ui->subject_lb->setText(subject) ;
                QString content = query.value(2).toString();
                QString content2 = QString("第%1题").arg(question_id-35) ;
                ui->textEdit_2->append(content2);
                ui->textEdit_2->append(content);
                QString choice = query.value(3).toString();
                choice.replace(" A","A");
                choice.replace("  ","\n");
                ui->textEdit_2->append(choice) ;
                QString answer = query.value(4).toString();
                ui->answer_lb->setText(answer) ;
        //        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

            }

        }
    }






}
//开始抢答
void ServerWidget::on_StartBtn_2_clicked()
{
    QString str = "stat" ;

    for(int i = 0 ; i < save_socketlist.size() ; i++)
    {

         save_socketlist[i]->write(str.toLocal8Bit()) ;


    }



    QSqlQuery query;
    int num1 = 1 ;
    QString str_que = QString("select * from question where id = %1").arg(num1) ;
    query.exec(str_que );
//    qDebug() << "22222" ;
    while(query.next())//一行一行去遍历数据
    {
        //打印数据
        int id = query.value(0).toInt();
        QString subject = query.value(1).toString();
        QString content = query.value(2).toString();
        QString choice = query.value(3).toString();
        QString answer = query.value(4).toString();
        qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;

    }
}

void ServerWidget::on_SendBtn_clicked()
{
    QSqlQuery query;
     QString str_que = QString("select * from question where id = %1").arg(question_id) ;
     query.exec(str_que );
     while(query.next())//一行一行去遍历数据
     {


         //打印数据
         int id = query.value(0).toInt();
         QString subject = query.value(1).toString();
         QString content = query.value(2).toString();
         QString choice = query.value(3).toString();
         QString answer = query.value(4).toString();
         qDebug() << "题号:" << id << "学科:" << subject << "题目:" << content << "选项:" << choice << "答案:" << answer ;


         QString str_qu = QString("ques+%1:").arg(question_id);
         //QString str_qu = QString("ques+1:");
         QString str_qu2 = content ;
         QString str_qu3 =str_qu +  str_qu2 + choice  ;

         qDebug()<<str_qu3 ;
         for(int i = 0 ; i < save_socketlist.size() ; i++)
         {

              save_socketlist[i]->write(str_qu3.toLocal8Bit()) ;


         }

     }


//    QString str = ""  题目;

//    socket->write()


    // 倒计时
    static  int time_1 = 10 ;   //修改时间
    time_1 = 10 ;
    QTimer *timer = new QTimer(this) ;
           timer->start(1000) ;
       // timerout
       QString str_tim = QString("%1s").arg(time_1) ;
       ui->label_14->setText(str_tim) ;


       connect(timer,&QTimer::timeout,[=](){

          time_1 -- ;
          QString str_tim = QString("%1s").arg(time_1) ;
          ui->label_14->setText(str_tim) ;
          if(time_1 < 0 )
          {
                        QString str_tim = QString("0s") ;
                        ui->label_14->setText(str_tim) ;
                        timer->stop() ;

          }
          if(time_1 == 0)
          {

             timer->stop() ;

//            socket->write(1) ;


          }
//          ui->label_2->setNum(num++) ;
       });
}



//结束按钮

void ServerWidget::on_EndBtn_3_clicked()
{

                 QThread::msleep(1000) ;

                 QString str = QString("scor+1:1") ;  //  QString("") ;

                 for(int i = 0 ; i < save_socketlist.size() ; i++)
                 {

                      save_socketlist[i]->write(str.toLocal8Bit()) ;


                 }


//                 for(int i = 0 ; i <= participants ; i++)
//                 {
//                         QThread::msleep(1000) ;
//                         QString str = QString("scor+%1:%2").arg(i).arg(score[i]) ;

//                         for(int j = 0 ; j < save_socketlist.size() ; j++)
//                         {

//                              save_socketlist[j]->write(str.toLocal8Bit()) ;


//                         }
//                 }





}


void ServerWidget::on_pushButton_clicked()
{
    QString str = QString("end*") ;


       for(int i = 0 ; i < save_socketlist.size() ; i++)
       {

            save_socketlist[i]->write(str.toLocal8Bit()) ;


       }
}

void ServerWidget::on_pushButton_2_clicked()
{
    QString str = ui->textEdit_3->toPlainText() ;

       qDebug() <<str ;


       for(int i = 0 ; i < save_socketlist.size() ; i++)
       {

            save_socketlist[i]->write(str.toLocal8Bit()) ;


       }
}

void ServerWidget::on_textEdit_3_copyAvailable(bool b)
{

}
